package com.itcast.rpc.client.connector.handler;

import com.itcast.common.data.RpcResponse;
import com.itcast.rpc.client.runner.RequestFuture;
import com.itcast.rpc.client.runner.RequestPool;
import com.itcast.rpc.client.runner.RpcRequestManager;
import com.itcast.rpc.client.runner.SyncRequestFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Rpc数据接收响应处理器
 */
@Component
@ChannelHandler.Sharable
public class RpcResponseHandler extends SimpleChannelInboundHandler<RpcResponse> {
    private static final Logger LOGGER = LoggerFactory.getLogger(RpcResponseHandler.class);

    @Autowired
    private RequestPool requestPool;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse znsResponse) throws Exception {
        // 获取请求回调信息
        requestPool.notifyRequest(znsResponse.getRequestId(),znsResponse);
    }


//    @Override
//    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse znsResponse) throws Exception {
//        // 获取请求回调信息
//        RequestFuture requestFuture = SyncRequestFuture.syncRequest.get(znsResponse.getRequestId());
//        if(null != requestFuture) {
//            // 设置回调结果
//            requestFuture.setResponse(znsResponse);
//        }else {
//            LOGGER.warn("未能找到回调信息！" + znsResponse);
//        }
//    }
}
